package com.wise.site.utils;

import java.util.Date;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.text.format.DateFormat;
import android.widget.Toast;

import com.wise.site.R;
import com.wise.site.WiseSiteApplication;
import com.wise.site.detail.BuyDetailsActivity;
import com.wise.site.protocol.action.CollectAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.CommentUpload;

/**
 * 收藏操作工具类
 * @author Administrator
 *
 */
public class CollectorUtils {
	private ProgressDialog mProgressDialog;
	private static CollectorUtils mCollectorUtils;
	private CollectAction mUploadAction;
	
	private CollectorUtils (){
		mUploadAction = new CollectAction(ACTION_TYPE.ACTION_COMMON, "collection");
	}
	
	public static CollectorUtils getInstance() {
		if(mCollectorUtils == null){
			mCollectorUtils = new CollectorUtils();
		}
		
		return mCollectorUtils;
	}
	
	ActionListener<CommentUpload> mActionListener = new ActionListener<CommentUpload>() {

			@Override
			public void onSucceed(CommentUpload result) {
				if(result.flag == 1){
					Toast.makeText(WiseSiteApplication.getContext(), R.string.collect_success, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(WiseSiteApplication.getContext(), result.msg, Toast.LENGTH_SHORT).show();
				}
				if(mProgressDialog != null && mProgressDialog.isShowing()){
					mProgressDialog.dismiss();
				}
			}

			@Override
			public void onError(int resultCode) {
				Toast.makeText(WiseSiteApplication.getContext(), "上传收藏错误", Toast.LENGTH_SHORT).show();
				if(mProgressDialog != null && mProgressDialog.isShowing()){
					mProgressDialog.dismiss();
				}
			}
			
		};
	
	/**
	 * 上传接口
	 * @param params
	 * @param listener
	 */
	public void uploadData(UploadData params, Context ctx) {
		mUploadAction.setActionListener(mActionListener);
		mUploadAction.addJsonParam("SubjectID", params.subjectId);
		mUploadAction.addJsonParam("Type", params.type);
		mUploadAction.addJsonParam("CuserId", params.cuserId);
		mUploadAction.addJsonParam("BuserId", params.buserId);
		mUploadAction.addJsonParam("PhoneType", 1);
		mUploadAction.addJsonParam("CreateDate", DateFormat.format("yyyy-MM-dd hh:mm:ss", new Date()));
		
		showProgress(ctx);
		
		ProtocolManager.getProtocolManager().submitAction(mUploadAction);
	}
	
	
	protected void showProgress(Context ctx) {
		try{
			if(mProgressDialog == null){
				mProgressDialog = ProgressDialog.show(ctx, null, "收藏保存中");
			}else if(!mProgressDialog.isShowing()){
				mProgressDialog.show();
			}
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					try{
						mProgressDialog.dismiss();
					}catch (Exception e) {
					}
				}
			}, 16000);
		}catch (Exception e) {
			mProgressDialog = null;
			showProgress(ctx);
			
		}
	}
	
	/**
	 * SubjectID 主题id
	 * Type 收集类型(1供应 2求购 3资讯 4店铺)
	 * CuserId 收藏人ID
	 * BuserId 被收藏人ID
	 * PhoneType 手机端类型( 1 Android 2 IOS)
	 * CreateDate 收集时间

	 * @author Administrator
	 *
	 */
	public static class UploadData {
		public UploadData() {
		}
		
		public UploadData(int subjectId, int type, int cuserId, int buserId) {
			this.subjectId = subjectId;
			this.type = type;
			this.cuserId = cuserId;
			this.buserId = buserId;
		}
		public int subjectId;
		public int type;
		public int cuserId;
		public int buserId;
	}
	
	
}
